import qs from '../../utils/axios';

const getTianQi=async ()=>{
	// http://v1.yiketianqi.com/api?unescape=1&version=v61&appid=38567298&appsecret=6niTYOWZ&city
	let data:any = await getCity()
	return qs.get(`/TQ/api?unescape=1&version=v3&appid=38567298&appsecret=6niTYOWZ&ip=${data.ip}`)
}

/**
 * {
    "ip": "218.68.183.158",
    "network": "218.68.128.0/18",
    "version": "IPv4",
    "city": "Tianjin",
    "region": "Tianjin",
    "region_code": "TJ",
    "country": "CN",
    "country_name": "China",
    "country_code": "CN",
    "country_code_iso3": "CHN",
    "country_capital": "Beijing",
    "country_tld": ".cn",
    "continent_code": "AS",
    "in_eu": false,
    "postal": null,
    "latitude": 39.1488,
    "longitude": 117.1762,
    "timezone": "Asia/Shanghai",
    "utc_offset": "+0800",
    "country_calling_code": "+86",
    "currency": "CNY",
    "currency_name": "Yuan Renminbi",
    "languages": "zh-CN,yue,wuu,dta,ug,za",
    "country_area": 9596960.0,
    "country_population": 1411778724,
    "asn": "AS4837",
    "org": "CHINA UNICOM China169 Backbone"
}
 */

// 根据当前ip地址 获取当前城市
const getCity = ()=>{
	return new Promise((resolve, reject) => {
		qs.get("https://ipapi.co/json/").then((data:any)=>{
			// console.log(data.ip,12)
			resolve(data)
		})
	});
}
// 首先，确保用户已经授权访问地理位置
// if (navigator.geolocation) {
//   navigator.geolocation.getCurrentPosition(function(position) {
//     // 获取用户的经度和纬度
//     var latitude = position.coords.latitude;
//     var longitude = position.coords.longitude;

//     // 使用天地图 API 获取用户所在的城市
//     var url = "https://api.tianditu.gov.cn/geocoder?lon=" + longitude + "&lat=" + latitude + "&key=您的天地图API密钥";
//     fetch(url)
//      .then(response => response.json())
//      .then(data => {
//         var city = data.result.addressComponent.city;
//         console.log("User is in " + city);
//       })
//      .catch(error => console.error(error));
//   }, function(error) {
//     console.log("Geolocation failed due to: " + error.message);
//   });
// } else {
//   console.log("Geolocation is not supported by this browser.");
// }
// 使用 ipapi.co API 获取当前 IP 地址的信息
// fetch("https://ipapi.co/json/")
//  .then(response => response.json())
//  .then(data => {
//     // 获取当前 IP 所在的国家、省份、城市和经纬度
//     var country = data.country;
//     var region = data.region;
//     var city = data.city;
//     var latitude = data.latitude;
//     var longitude = data.longitude;

//     // 使用 Google Maps API 获取城市的详细信息
//     var geocoder = new google.maps.Geocoder();
//     geocoder.geocode({
//       'address': city
//     }, function(results, status) {
//       if (status == google.maps.GeocoderStatus.OK) {
//         if (results[0]) {
//           var city = results[0].formatted_address;
//           var location = results[0].geometry.location;
//           var lat = location.lat();
//           var lng = location.lng();
//           console.log("User is in " + city + " (" + lat + ", " + lng + ")");
//         } else {
//           console.log("No results found");
//         }
//       } else {
//         console.log("Geocoder failed due to: " + status);
//       }
//     });
//   })
//  .catch(error => console.error(error));

export default{
	getTianQi,
	getCity
}